package io.renren.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.SysUserEntity;
import io.renren.entity.TPurviewCustinfoEntity;
import io.renren.entity.TPurviewCustprotocolEntity;
import io.renren.service.TPurviewCustinfoService;
import io.renren.service.TPurviewCustprotocolService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import io.renren.utils.ShiroUtils;


/**
 * 客户协议
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-02-06 17:41:04
 */
@RestController
@RequestMapping("tpurviewcustprotocol")
public class TPurviewCustprotocolController {
	@Autowired
	private TPurviewCustprotocolService tPurviewCustprotocolService;
	@Autowired
	private TPurviewCustinfoService tPurviewCustinfoService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("tpurviewcustprotocol:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<TPurviewCustprotocolEntity> tPurviewCustprotocolList = tPurviewCustprotocolService.queryList(query);
		int total = tPurviewCustprotocolService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(tPurviewCustprotocolList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("tpurviewcustprotocol:info")
	public R info(@PathVariable("id") String id){
		TPurviewCustprotocolEntity tPurviewCustprotocol = tPurviewCustprotocolService.queryObject(id);
		
		return R.ok().put("tPurviewCustprotocol", tPurviewCustprotocol);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("tpurviewcustprotocol:save")
	public R save(@RequestBody TPurviewCustprotocolEntity tPurviewCustprotocol){
		TPurviewCustinfoEntity tPurviewCustinfoEntity =tPurviewCustinfoService.queryObject(tPurviewCustprotocol.getCustid());
		SysUserEntity obj = ShiroUtils.getUserEntity();
		tPurviewCustprotocol.setCreatename(obj.getUsername());
		tPurviewCustprotocol.setCreatedate(new Date());
		tPurviewCustprotocol.setCropname(tPurviewCustinfoEntity.getCropname());
		tPurviewCustprotocol.setStatus("0");
		tPurviewCustprotocolService.save(tPurviewCustprotocol);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("tpurviewcustprotocol:update")
	public R update(@RequestBody TPurviewCustprotocolEntity tPurviewCustprotocol){
		tPurviewCustprotocolService.update(tPurviewCustprotocol);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("tpurviewcustprotocol:delete")
	public R delete(@RequestBody String[] ids){
		tPurviewCustprotocolService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
